package com.emo.wowoblog.controller;


import com.emo.wowoblog.entity.Catalog;
import com.emo.wowoblog.service.CatalogService;
import com.emo.wowoblog.view.CatalogView;
import com.emo.wowoblog.vo.HttpResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/catalog")
public class CatalogController  {

    @Resource
    private CatalogService catalogService;

    @GetMapping("queryTopCatalog")
    public Map<String,Object> queryTopCatalog(){
        try{
            List<Catalog> catalogList = catalogService.getCatalogsByPid(0);
            if(catalogList!=null){
                return HttpResult.success(catalogList);
            }else{
                return HttpResult.fail();
            }

        }catch(Exception ex){
            ex.printStackTrace();
            return HttpResult.fail();
        }
    }

    @GetMapping("/initCatalogs")
    public Map<String,Object> initCatalogs(){
        CatalogView cv= null;
        try{

//            if (redisTemplate.hasKey("catalogView")) {
//                System.out.println("读取缓存菜单资料");
//                cv = (CatalogView) redisTemplate.opsForValue().get("catalogView");
//            } else {
//                System.out.println("从数据库中读取菜单资料");
                cv = catalogService.initCatalogView();
//                redisTemplate.opsForValue().set("catalogView", cv);
//            }

            if(cv!=null){
                return HttpResult.success(cv);
            }else{
                return HttpResult.fail();
            }

        }catch(Exception ex){
            ex.printStackTrace();
            return HttpResult.fail();
        }
    }
}
